<?php
/** *
 * 数据字典挂件
 */

namespace Todo\Widget;
use Think\Controller;
use Todo\Service\DictService;

class DictWidget extends Controller{
	/**
	 * 生成单选框
	 * @param string $code 字典代码
	 * @param string $name 单选框名称
	 * @param mixed $value 默认选中的值
	 */
	public function radio($code, $name, $value){
		$dict = DictService::getInstance()->getDict($code);
		$this->assign('name', $name);
		$this->assign('value', $value);
		$this->assign('dict', $dict['map']);
		$this->display('./Common/View/Widget/dict_radio.html');
	}

	/**
	 * 生成多选框
	 * @param string $code 字典代码
	 * @param string $name 单选框名称
	 * @param mixed $value 默认选中的值
	 */
	public function checkbox($code, $name, $value){
		$dict = DictService::getInstance()->getDict($code);
		$this->assign('name', $name);
		if(is_array($value)){
			$this->assign('value', implode(',', $value));
			$this->assign('valArr', $value);
		}else{
			$this->assign('value', $value);
			$this->assign('valArr', explode(',', $value));
		}
		$this->assign('dict', $dict['map']);
		$this->display('./Common/View/Widget/dict_checkbox.html');
	}

	/**
	 * 生成下拉框
	 * @param string $code 字典代码
	 * @param mixed $value 默认选中的值
	 */
	public function select($code, $value){
		$dict = DictService::getInstance()->getDict($code);
		$this->assign('value', $value);
		$this->assign('dict', $dict['map']);
		$this->display('./Common/View/Widget/dict_select.html');
	}
}